﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace An_Im.Theme.Custom
{

    /// <summary>
    /// 可调节的文本框。
    /// </summary>
    public class AnAdjustableTextBox : AnTextBox
    {

        #region child controls

        private Button InAbsButton1, InAbsButton2, DeAbsButton1, DeAbsButton2, InRelButton1, DeRelButton1;

        #endregion


        #region properties



        public string ButtonText1
        {
            get { return (string)GetValue(ButtonText1Property); }
            set { SetValue(ButtonText1Property, value); }
        }

        // Using a DependencyProperty as the backing store for ButtonText1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonText1Property =
            DependencyProperty.Register("ButtonText1", typeof(string), typeof(AnAdjustableTextBox), new PropertyMetadata("1"));

        public string ButtonText2
        {
            get { return (string)GetValue(ButtonText2Property); }
            set { SetValue(ButtonText2Property, value); }
        }

        // Using a DependencyProperty as the backing store for ButtonText2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonText2Property =
            DependencyProperty.Register("ButtonText2", typeof(string), typeof(AnAdjustableTextBox), new PropertyMetadata("2"));

        public string ButtonText3
        {
            get { return (string)GetValue(ButtonText3Property); }
            set { SetValue(ButtonText3Property, value); }
        }

        // Using a DependencyProperty as the backing store for ButtonText3.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonText3Property =
            DependencyProperty.Register("ButtonText3", typeof(string), typeof(AnAdjustableTextBox), new PropertyMetadata("3"));

        public string ButtonText4
        {
            get { return (string)GetValue(ButtonText4Property); }
            set { SetValue(ButtonText4Property, value); }
        }

        // Using a DependencyProperty as the backing store for ButtonText4.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonText4Property =
            DependencyProperty.Register("ButtonText4", typeof(string), typeof(AnAdjustableTextBox), new PropertyMetadata("4"));




        public float LargeChange
        {
            get { return (float)GetValue(LargeChangeProperty); }
            set { SetValue(LargeChangeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LargeChange.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LargeChangeProperty =
            DependencyProperty.Register("LargeChange", typeof(float), typeof(AnAdjustableTextBox), new PropertyMetadata(0.25f));


        public float SmallChange
        {
            get { return (float)GetValue(SmallChangeProperty); }
            set { SetValue(SmallChangeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SmallChange.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SmallChangeProperty =
            DependencyProperty.Register("SmallChange", typeof(float), typeof(AnAdjustableTextBox), new PropertyMetadata(0.05f));



        /// <summary>
        /// 是否是合并模式、即按钮的状态。
        /// </summary>
        public bool IsMergedMode
        {
            get { return (bool)GetValue(IsMergedModeProperty); }
            set { SetValue(IsMergedModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsMergedMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsMergedModeProperty =
            DependencyProperty.Register("IsMergedMode", typeof(bool), typeof(AnAdjustableTextBox), new PropertyMetadata(true));

        #endregion


        #region methods


        public void IncreaseValueLarge()
        {
            var val = GetValidTextValue();
            val = Math.Min(MaxValue, val + LargeChange);
            Text = val.ToString();
        }

        public void IncreaseValueSmall()
        {
            var val = GetValidTextValue();
            val = Math.Min(MaxValue, val + SmallChange);
            Text = val.ToString();
        }

        public void DecreaseValueLarge()
        {
            var val = GetValidTextValue();
            val = Math.Max(MinValue, val - LargeChange);
            Text = val.ToString();
        }


        public void DecreaseValueSmall()
        {
            var val = GetValidTextValue();
            val = Math.Max(MinValue, val - SmallChange);
            Text = val.ToString();
        }
        #endregion


        #region ctors
        public AnAdjustableTextBox()
        {
            OnlyNumber = true;

        }



        static AnAdjustableTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AnAdjustableTextBox), new FrameworkPropertyMetadata(typeof(AnAdjustableTextBox)));
        }
        #endregion


        #region override

        public override void EndInit()
        {
            base.EndInit();

        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            InAbsButton1 = (Button)GetTemplateChild("inabsButton1");
            InAbsButton2 = (Button)GetTemplateChild("inabsButton2");
            DeAbsButton1 = (Button)GetTemplateChild("deabsButton1");
            DeAbsButton2 = (Button)GetTemplateChild("deabsButton2");
            InRelButton1 = (Button)GetTemplateChild("inrelButton1");
            DeRelButton1 = (Button)GetTemplateChild("derelButton1");


            InAbsButton1.Click += OnIncreaseAbsValue;
            InAbsButton2.Click += OnIncreaseAbsValue;
            DeAbsButton1.Click += OnDecreaseAbsValue;
            DeAbsButton2.Click += OnDecreaseAbsValue;
            InRelButton1.Click += OnIncreaseRelValue;
            DeRelButton1.Click += OnDecreaseRelValue;


        }
        #endregion

        #region events

        #region absvalue+


        private void AnAdjustableTextBox_IncreaseAbsValue(object sender, RoutedEventArgs e)
        {
            var ispress = Keyboard.IsKeyDown(Key.LeftCtrl);
            if (IsMergedMode)
            {
                if (ispress)
                {
                    IncreaseValueSmall();
                }
                else
                {
                    IncreaseValueLarge();
                }
            }
            else
            {

                if (ispress)
                {
                    IncreaseValueSmall();
                }
                else
                {
                    IncreaseValueLarge();
                }
            }

        }

        public void OnIncreaseAbsValue(object sender, EventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(IncreaseAbsValueEvent, this);
            RaiseEvent(args);
        }


        public static readonly RoutedEvent IncreaseAbsValueEvent =
                  EventManager.RegisterRoutedEvent("IncreaseAbsValue", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AnAdjustableTextBox));

        /// <summary>
        /// 注册.
        /// </summary>
        public event RoutedEventHandler IncreaseAbsValue
        {
            add
            {
                AddHandler(IncreaseAbsValueEvent, value);
            }

            remove
            {
                RemoveHandler(IncreaseAbsValueEvent, value);
            }
        }
        #endregion


        #region absvalue-

        private void AnAdjustableTextBox_DecreaseAbsValue(object sender, RoutedEventArgs e)
        {
            var ispress = Keyboard.IsKeyDown(Key.LeftCtrl);
            if (IsMergedMode)
            {
                if (ispress)
                {
                    DecreaseValueSmall();
                }
                else
                {
                    DecreaseValueLarge();
                }
            }
            else
            {

                if (ispress)
                {
                    DecreaseValueSmall();
                }
                else
                {
                    DecreaseValueLarge();
                }
            }

        }


        public void OnDecreaseAbsValue(object sender, EventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(DecreaseAbsValueEvent, this);
            RaiseEvent(args);
        }


        public static readonly RoutedEvent DecreaseAbsValueEvent =
                  EventManager.RegisterRoutedEvent("DecreaseAbsValue", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AnAdjustableTextBox));

        /// <summary>
        /// 注册.
        /// </summary>
        public event RoutedEventHandler DecreaseAbsValue
        {
            add
            {
                AddHandler(DecreaseAbsValueEvent, value);
            }

            remove
            {
                RemoveHandler(DecreaseAbsValueEvent, value);
            }
        }

        #endregion

        #region relvalue+

        private void AnAdjustableTextBox_IncreaseRelValue(object sender, RoutedEventArgs e)
        {

        }

        public void OnIncreaseRelValue(object sender, EventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(IncreaseRelValueEvent, this);
            RaiseEvent(args);
        }


        public static readonly RoutedEvent IncreaseRelValueEvent =
                  EventManager.RegisterRoutedEvent("IncreaseRelValue", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AnAdjustableTextBox));

        /// <summary>
        /// 注册.
        /// </summary>
        public event RoutedEventHandler IncreaseRelValue
        {
            add
            {
                AddHandler(IncreaseRelValueEvent, value);
            }

            remove
            {
                RemoveHandler(IncreaseRelValueEvent, value);
            }
        }

        #endregion

        #region relvalue-

        private void AnAdjustableTextBox_DecreaseRelValue(object sender, RoutedEventArgs e)
        {


        }

        public void OnDecreaseRelValue(object sender, EventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(DecreaseRelValueEvent, this);
            RaiseEvent(args);
        }


        public static readonly RoutedEvent DecreaseRelValueEvent =
                  EventManager.RegisterRoutedEvent("DecreaseRelValue", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AnAdjustableTextBox));

        /// <summary>
        /// 注册.
        /// </summary>
        public event RoutedEventHandler DecreaseRelValue
        {
            add
            {
                AddHandler(DecreaseRelValueEvent, value);
            }

            remove
            {
                RemoveHandler(DecreaseRelValueEvent, value);
            }
        }

        #endregion


        #endregion

    }
}


